<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>
	.KEYW {color: #933;}
	.COMM {color: #bbb; font-style: italic;}
	.NUMB {color: #393;}
	.STRN {color: #393;}
	.REGX {color: #339;}
	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
	</style></head><body><pre><span class='line'>  1</span> <span class="COMM">/**
<span class='line'>  2</span>  * Returns a {@link pv.Tree} operator for the specified array. This is a
<span class='line'>  3</span>  * convenience factory method, equivalent to &lt;tt>new pv.Tree(array)&lt;/tt>.
<span class='line'>  4</span>  *
<span class='line'>  5</span>  * @see pv.Tree
<span class='line'>  6</span>  * @param {array} array an array from which to construct a tree.
<span class='line'>  7</span>  * @returns {pv.Tree} a tree operator for the specified array.
<span class='line'>  8</span>  */</span><span class="WHIT">
<span class='line'>  9</span> </span><span class="NAME">pv.tree</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">array</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 10</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">new</span><span class="WHIT"> </span><span class="NAME">pv.Tree</span><span class="PUNC">(</span><span class="NAME">array</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 11</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 12</span> 
<span class='line'> 13</span> </span><span class="COMM">/**
<span class='line'> 14</span>  * Constructs a tree operator for the specified array. This constructor should
<span class='line'> 15</span>  * not be invoked directly; use {@link pv.tree} instead.
<span class='line'> 16</span>  *
<span class='line'> 17</span>  * @class Represents a tree operator for the specified array. The tree operator
<span class='line'> 18</span>  * allows a hierarchical map to be constructed from an array; it is similar to
<span class='line'> 19</span>  * the {@link pv.Nest} operator, except the hierarchy is derived dynamically
<span class='line'> 20</span>  * from the array elements.
<span class='line'> 21</span>  *
<span class='line'> 22</span>  * &lt;p>For example, given an array of size information for ActionScript classes:
<span class='line'> 23</span>  *
<span class='line'> 24</span>  * &lt;pre>{ name: "flare.flex.FlareVis", size: 4116 },
<span class='line'> 25</span>  * { name: "flare.physics.DragForce", size: 1082 },
<span class='line'> 26</span>  * { name: "flare.physics.GravityForce", size: 1336 }, ...&lt;/pre>
<span class='line'> 27</span>  *
<span class='line'> 28</span>  * To facilitate visualization, it may be useful to nest the elements by their
<span class='line'> 29</span>  * package hierarchy:
<span class='line'> 30</span>  *
<span class='line'> 31</span>  * &lt;pre>var tree = pv.tree(classes)
<span class='line'> 32</span>  *     .keys(function(d) d.name.split("."))
<span class='line'> 33</span>  *     .map();&lt;/pre>
<span class='line'> 34</span>  *
<span class='line'> 35</span>  * The resulting tree is:
<span class='line'> 36</span>  *
<span class='line'> 37</span>  * &lt;pre>{ flare: {
<span class='line'> 38</span>  *     flex: {
<span class='line'> 39</span>  *       FlareVis: {
<span class='line'> 40</span>  *         name: "flare.flex.FlareVis",
<span class='line'> 41</span>  *         size: 4116 } },
<span class='line'> 42</span>  *     physics: {
<span class='line'> 43</span>  *       DragForce: {
<span class='line'> 44</span>  *         name: "flare.physics.DragForce",
<span class='line'> 45</span>  *         size: 1082 },
<span class='line'> 46</span>  *       GravityForce: {
<span class='line'> 47</span>  *         name: "flare.physics.GravityForce",
<span class='line'> 48</span>  *         size: 1336 } },
<span class='line'> 49</span>  *     ... } }&lt;/pre>
<span class='line'> 50</span>  *
<span class='line'> 51</span>  * By specifying a value function,
<span class='line'> 52</span>  *
<span class='line'> 53</span>  * &lt;pre>var tree = pv.tree(classes)
<span class='line'> 54</span>  *     .keys(function(d) d.name.split("."))
<span class='line'> 55</span>  *     .value(function(d) d.size)
<span class='line'> 56</span>  *     .map();&lt;/pre>
<span class='line'> 57</span>  *
<span class='line'> 58</span>  * we can further eliminate redundant data:
<span class='line'> 59</span>  *
<span class='line'> 60</span>  * &lt;pre>{ flare: {
<span class='line'> 61</span>  *     flex: {
<span class='line'> 62</span>  *       FlareVis: 4116 },
<span class='line'> 63</span>  *     physics: {
<span class='line'> 64</span>  *       DragForce: 1082,
<span class='line'> 65</span>  *       GravityForce: 1336 },
<span class='line'> 66</span>  *   ... } }&lt;/pre>
<span class='line'> 67</span>  *
<span class='line'> 68</span>  * For visualizations with large data sets, performance improvements may be seen
<span class='line'> 69</span>  * by storing the data in a tree format, and then flattening it into an array at
<span class='line'> 70</span>  * runtime with {@link pv.Flatten}.
<span class='line'> 71</span>  *
<span class='line'> 72</span>  * @param {array} array an array from which to construct a tree.
<span class='line'> 73</span>  */</span><span class="WHIT">
<span class='line'> 74</span> </span><span class="NAME">pv.Tree</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">array</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 75</span> </span><span class="WHIT">  </span><span class="NAME">this.array</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">array</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 76</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 77</span> 
<span class='line'> 78</span> </span><span class="COMM">/**
<span class='line'> 79</span>  * Assigns a &lt;i>keys&lt;/i> function to this operator; required. The keys function
<span class='line'> 80</span>  * returns an array of &lt;tt>string&lt;/tt>s for each element in the associated
<span class='line'> 81</span>  * array; these keys determine how the elements are nested in the tree. The
<span class='line'> 82</span>  * returned keys should be unique for each element in the array; otherwise, the
<span class='line'> 83</span>  * behavior of this operator is undefined.
<span class='line'> 84</span>  *
<span class='line'> 85</span>  * @param {function} k the keys function.
<span class='line'> 86</span>  * @returns {pv.Tree} this.
<span class='line'> 87</span>  */</span><span class="WHIT">
<span class='line'> 88</span> </span><span class="NAME">pv.Tree.prototype.keys</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">k</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 89</span> </span><span class="WHIT">  </span><span class="NAME">this.k</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">k</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 90</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">this</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 91</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 92</span> 
<span class='line'> 93</span> </span><span class="COMM">/**
<span class='line'> 94</span>  * Assigns a &lt;i>value&lt;/i> function to this operator; optional. The value
<span class='line'> 95</span>  * function specifies an optional transformation of the element in the array
<span class='line'> 96</span>  * before it is inserted into the map. If no value function is specified, it is
<span class='line'> 97</span>  * equivalent to using the identity function.
<span class='line'> 98</span>  *
<span class='line'> 99</span>  * @param {function} k the value function.
<span class='line'>100</span>  * @returns {pv.Tree} this.
<span class='line'>101</span>  */</span><span class="WHIT">
<span class='line'>102</span> </span><span class="NAME">pv.Tree.prototype.value</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">v</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>103</span> </span><span class="WHIT">  </span><span class="NAME">this.v</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">v</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>104</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">this</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>105</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>106</span> 
<span class='line'>107</span> </span><span class="COMM">/**
<span class='line'>108</span>  * Returns a hierarchical map of values. The hierarchy is determined by the keys
<span class='line'>109</span>  * function; the values in the map are determined by the value function.
<span class='line'>110</span>  *
<span class='line'>111</span>  * @returns a hierarchical map of values.
<span class='line'>112</span>  */</span><span class="WHIT">
<span class='line'>113</span> </span><span class="NAME">pv.Tree.prototype.map</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>114</span> </span><span class="WHIT">  </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">map</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">o</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>115</span> </span><span class="WHIT">  </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">this.array.length</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>116</span> </span><span class="WHIT">    </span><span class="NAME">o.index</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>117</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">value</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.array</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">keys</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.k.call</span><span class="PUNC">(</span><span class="NAME">o</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">value</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">node</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">map</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>118</span> </span><span class="WHIT">    </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">j</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">j</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">keys.length</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">j</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>119</span> </span><span class="WHIT">      </span><span class="NAME">node</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">node</span><span class="PUNC">[</span><span class="NAME">keys</span><span class="PUNC">[</span><span class="NAME">j</span><span class="PUNC">]</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">node</span><span class="PUNC">[</span><span class="NAME">keys</span><span class="PUNC">[</span><span class="NAME">j</span><span class="PUNC">]</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>120</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>121</span> </span><span class="WHIT">    </span><span class="NAME">node</span><span class="PUNC">[</span><span class="NAME">keys</span><span class="PUNC">[</span><span class="NAME">j</span><span class="PUNC">]</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.v</span><span class="WHIT"> </span><span class="PUNC">?</span><span class="WHIT"> </span><span class="NAME">this.v.call</span><span class="PUNC">(</span><span class="NAME">o</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">value</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">value</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>122</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>123</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">map</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>124</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>125</span> </span></pre></body></html>